import { IapNode } from '../parse';
import { IapBody, BaseIap, IapStep, IapButton, Bound, Vector2, IapOption } from './gameClass';

var id = 0;
function nextID(): string {
    return "iapGID-"+ id++;
}

export function varFAndNodeConvertToIap(root: IapNode) {
    //变量功能
    varF(root, root);
    console.log("变量赋值后", root);
    //变量功能 end
    //属性检查 防止执行js脚步

    //end 属性检查
    
    //cp 功能
    var idMap : {[x: string]: IapNode} = {};
    root.depthForEach(function(node) {
        if (!node.getAttr("i")) {
            node.setAttr("i", nextID()); 
        }
        idMap[node.getAttr("i")!] = node;
    })
    root.depthForEach(function(node){
        if (node.getAttr("cp")) {
            var cpTarget = idMap[node.getAttr("cp")!]
            if (cpTarget) {
                for (let cpAttrKey in cpTarget.attrs) {
                    if (cpAttrKey != "i" && !node.getAttr(cpAttrKey)) {
                        node.setAttr(cpAttrKey, cpTarget.getAttr(cpAttrKey)!);
                    }
                }
            }
        }
    })
    //cp end

    var body = nodeConvertToIap(root);

    return body;
}


function nodeConvertToIap(node: IapNode): BaseIap | undefined {
    var iap = parseNode(node);
    if(iap) {
        for (let child of node.children) {
            var iap2 = nodeConvertToIap(child)
            if (iap2) {
                iap.children.push(iap2);
            }
        }
    }
    return iap;
}

function parseNode(node: IapNode) : BaseIap | undefined {
    var tag = node.tag;
    var iap ;
    switch(tag) {  
        case ":IAP":
            iap = new IapBody();
            iap.cover = node.getAttr("bg")!;
            iap.nextStepID = node.getAttr("n", "next")!;
            iap.size = parseVector2(node.getAttr("size"))!;
            iap.fontSize = node.numberAttr("font-size")!;
            break;
        case "step": 
            iap = new IapStep();
            iap.bg = node.getAttr("bg");
            iap.bgm = node.getAttr("bgm");
            iap.nextStepID = node.getAttr("n"); 
            iap.se = node.getAttr("se");
            iap.effects = node.arrayAttr("effect")!;
            iap.video = node.getAttr("video");
            iap.loop = node.boolAttr("loop");
            iap.galText = node.getAttr("tx", "text");
            iap.enterDestroy = node.arrayAttr("edy", "enter-destroy")!;
            iap.leaveDestroy = node.arrayAttr("ldy", "leave-destroy")!;
            iap.duration = node.numberAttr("dur", "duration")!;
            break;
        case "btn": 
            iap = new IapButton();
            iap.bound = parseBound(node)!;
            iap.icon = node.getAttr("icon");
            iap.text = node.getAttr("tx");
            iap.touch = node.boolAttr("touch");
            iap.se = node.getAttr("se");
            iap.nextStepID = node.getAttr("n");
            iap.hoverText = node.getAttr("hover");
            iap.loop = node.boolAttr("loop");
            iap.effects = node.arrayAttr("effect")!;
            iap.clickDestroy = node.arrayAttr("cdy", "click-destroy")!;
            iap.bg = node.getAttr("bg")!;
            iap.writingMode = node.getAttr("wm", "writing-mode")!;
            iap.textAlgin = node.getAttr("ta", "text-algin")!;
            break;
        case "opt": 
            iap = new IapOption();
            iap.text = node.getAttr("tx")!;
            iap.nextStepID = node.getAttr("n")!;
            break;
    }
    if(iap) {
        iap.id = node.getAttr("i")!;
        iap.notDestroy = node.boolAttr("ndy", "not-destroy")!;
        iap.fontSize = node.numberAttr("fs", "font-size")!;
    }
    return iap;
}


export function parseBound(node: IapNode): Bound | undefined {
    var bound;
    if (node.getAttr('l') || node.getAttr('r') || node.getAttr('b') || node.getAttr('t') || node.getAttr('w') || node.getAttr('h')) {
        bound = new Bound();
        bound.left = node.numberAttr('l')!;
        bound.right = node.numberAttr('r')!;
        bound.bottom = node.numberAttr('b')!;
        bound.top = node.numberAttr('t')!;
        bound.width = node.numberAttr('w')!;
        bound.height = node.numberAttr('h')!;
    }
    return bound;
}

export function parseVector2(str: string | undefined): Vector2 | undefined {
    if(!str) {
        return undefined;
    }
    var split = str.split(",")
    
    return new Vector2(parseFloat(split[0]), parseFloat(split[1]));
}

export function varF(current: IapNode, root: IapNode) {
    //变量功能
    for (var key in current.attrs) {
        var value = current.attrs[key];
        var regx = /\$(.*?)%/g;
        var str = '';
        var lastIndex = 0;
        do {
            var res = regx.exec(value);
            if (res) {
                if (root.getAttr('c-' + res[1])) {
                    if (lastIndex != res.index) {
                        str += value.substring(lastIndex, res.index);
                    }
                    str += root.getAttr('c-' + res[1]);
                    lastIndex = res.index + res[0].length;
                }
            }
            else if (lastIndex != 0 && lastIndex != value.length) {
                str += value.substring(lastIndex, value.length);
            }
        } while (res && lastIndex != value.length);
        if (lastIndex != 0) {
            current.setAttr(key, str);
        }
    }
    for (let child of current.children) {
        varF(child, root);
    }
}
